package com.kkb.controller;

import com.github.pagehelper.PageInfo;
import com.kkb.pojo.HosRegister;
import com.kkb.service.HosRegisterService;
import com.kkb.vo.HostRegisterQueryVO;
import com.kkb.vo.ResultVO;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
@ResponseBody
@RequestMapping("register")
public class HosRegisterController {

    @Resource
    HosRegisterService hosRegisterService;

    @RequestMapping(value = "{id}", method = RequestMethod.GET)
    public ResultVO queryById(@PathVariable("id") int id) {
        HosRegister hos = hosRegisterService.queryById(id);
        return hos != null ? new ResultVO<>(hos) : new ResultVO<>(404, "未找到挂号订单");
    }

    @RequestMapping(value = "", method = RequestMethod.GET)
    public ResultVO queryByIdList(@RequestParam("ids") List<Integer> ids) {
        List result = hosRegisterService.queryByIdList(ids);
        return result != null ? new ResultVO<>(result) : new ResultVO<>(500, "服务器异常");
    }

    @RequestMapping(value = "list", method = RequestMethod.GET)
    public ResultVO<HosRegister> selectInPage(Integer index, Integer size,
                                              HostRegisterQueryVO hostRegisterQueryVO){

        PageInfo<HosRegister> hosRegisterPageInfo = hosRegisterService.selectInPage(index, size, hostRegisterQueryVO);
        return hosRegisterPageInfo!=null ? new ResultVO<>(hosRegisterPageInfo):new ResultVO<>(404,"暂无信息");
    }

    @RequestMapping(value = "{id}", method = RequestMethod.PUT)
    public ResultVO<HosRegister> quitState(@PathVariable("id") Integer id){
        return hosRegisterService.quitState(id)>0 ? new ResultVO<>():new ResultVO<>(500,"退号失败，服务器异常");
    }

    @RequestMapping(value = "", method = RequestMethod.PUT)
    public ResultVO<HosRegister> updateRegister(HosRegister hosRegister, Integer departmentId, Integer doctorId) {
        return hosRegisterService.updateHosRegister(hosRegister,doctorId)>0?
                new ResultVO<>():new ResultVO<>(500,"更新失败");
    }

    @RequestMapping(value = "",method = RequestMethod.POST)
    public ResultVO<HosRegister> insertRegister(HosRegister hosRegister){
        return hosRegisterService.insertRegister(hosRegister)>0?
                new ResultVO<>():new ResultVO<>(500,"新增失败，服务器异常");
    }

    @RequestMapping(value = "list",method = RequestMethod.DELETE)
    public ResultVO<HosRegister> quitSelected(@RequestParam("check") List<Integer> check){
        int sum =0;
        for (Integer integer : check) {
            int i = hosRegisterService.quitState(integer);
            sum+=i;
        }
        return sum > 0 ? new ResultVO<>(): new ResultVO<>(500,"退号失败，服务器异常");
    }
}
